package pub.tbc.hh.mall.schedule;

import org.springframework.stereotype.Service;
import pub.tbc.toolkit.core.thread.ExecutorFactory;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author tbc
 */
@Service("delayExecService")
public class DefaultDelayExecServiceImpl implements DelayExecService {
    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors(),
            ExecutorFactory.createThreadFactory("delayThreadPool-%d")
    );

    @Override
    public void execute(Runnable runnable) {
        executor.execute(runnable);
    }

    @Override
    public void execute(Runnable runnable, long millisTime) {
        execute(runnable, millisTime, TimeUnit.MILLISECONDS);
    }

    @Override
    public void execute(Runnable runnable, long time, TimeUnit timeUnit) {
        executor.schedule(runnable, time, timeUnit);
    }
}
